//
//  BindThirdViewController.m
//  GainInvest
//
//  Created by 王玉龙 on 17/3/24.
//  Copyright © 2017年 longlong. All rights reserved.
//

#define CellIdentifer @"UITableViewCell"


#import "BindThirdViewController.h"
#import "UserInfoHttpManager.h"
#import <UMSocialCore/UMSocialCore.h>

@interface BindThirdViewController ()
<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic ,strong) UIView *footerView;
@property (nonatomic ,strong) UITableView *tableview;

@property (nonatomic ,strong) UserInfoHttpManager *httpManager;

@end

@implementation BindThirdViewController

- (void)dealloc
{
    _httpManager = nil;
}

- (UserInfoHttpManager *)httpManager
{
    if (_httpManager == nil)
    {
        _httpManager = [[UserInfoHttpManager alloc]init];
    }
    
    return _httpManager;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self customNavBar];
    
    [self.view addSubview:self.tableview];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)customNavBar
{
    self.navigationItem.title = @"账号管理";
    
    LeftBackItem *leftBarItem = [[LeftBackItem alloc] initWithTarget:self Selector:@selector(leftNavBarButtonClick)];
    self.navigationItem.leftBarButtonItem=leftBarItem;
    
}

- (void)leftNavBarButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableView *)tableview
{
    if (_tableview == nil)
    {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.rowHeight = 50;
        _tableview.tableFooterView = [UIView new];
    }
    return _tableview;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifer];
        
        cell.textLabel.textColor = TextColorBlack;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.detailTextLabel.textColor = TextColorBlack;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    }
    
    
    AccountInfo *account = [AccountInfo standardAccountInfo];
    

    if (indexPath.row == 0)
    {
        cell.textLabel.text = @"微信快捷登录";
        
        if (account.weChatUid && account.weChatUid.length > 0)
        {
            cell.detailTextLabel.text = @"已绑定";
             cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            cell.detailTextLabel.text = @"去绑定";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    else
    {
        cell.textLabel.text = @"QQ快捷登录";
        if (account.qqUid && account.qqUid.length > 0)
        {
            cell.detailTextLabel.text = @"已绑定";
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            cell.detailTextLabel.text = @"去绑定";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    AccountInfo *account = [AccountInfo standardAccountInfo];
    if (indexPath.row == 0 && !(account.weChatUid && account.weChatUid.length > 0))
    {
        //微信快捷登录
        [self thirdPartyLoginWithPlayform:UMSocialPlatformType_WechatSession Type:@"1"];
    }
    else if (indexPath.row == 1 && !(account.qqUid && account.qqUid.length > 0))
    {
        //QQ快捷登录
        [self thirdPartyLoginWithPlayform:UMSocialPlatformType_QQ Type:@"2"];
    }
}

- (void)thirdPartyLoginWithPlayform:(UMSocialPlatformType)platformType Type:(NSString *)type
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error)
     {
         UMSocialUserInfoResponse *resp = result;
         
         NSLog(@"error ====== %@",error);
         
         // 第三方登录数据(为空表示平台未提供)
         // 授权数据
         NSLog(@" uid: %@", resp.uid);
         NSLog(@" openid: %@", resp.openid);
         NSLog(@" accessToken: %@", resp.accessToken);
         NSLog(@" refreshToken: %@", resp.refreshToken);
         NSLog(@" expiration: %@", resp.expiration);
         
         // 用户数据
         NSLog(@" name: %@", resp.name);
         NSLog(@" iconurl: %@", resp.iconurl);
         NSLog(@" gender: %@", resp.gender);
         
         // 第三方平台SDK原始数据
         NSLog(@" originalResponse: %@", resp.originalResponse);
         
         if (resp.uid)
         {
             [self thirdPartyLoginWithUid:resp.uid Type:type];
         }
     }];
    
}

- (void)thirdPartyLoginWithUid:(NSString *)uid Type:(NSString *)type
{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"设置中...";
    
    AccountInfo *account = [AccountInfo standardAccountInfo];
    
    NSDictionary *parameterDict = @{};
    
    //微信是1qq是2*
    if ([type isEqualToString:@"1"])
    {
        parameterDict = @{@"user_id":account.internalBaseClassIdentifier,@"birthday":account.birthday,@"nickname":account.nickname,@"sex":account.sex,@"minename":account.realname,@"headimg":account.head,@"qq_openid":account.qqUid,@"weixin_openid":uid,@"is_trad_rg":account.isOpenAccount};

    }
    else if ([type isEqualToString:@"2"])
    {
        parameterDict = @{@"user_id":account.internalBaseClassIdentifier,@"birthday":account.birthday,@"nickname":account.nickname,@"sex":account.sex,@"minename":account.realname,@"headimg":account.head,@"qq_openid":uid,@"weixin_openid":account.weChatUid,@"is_trad_rg":account.isOpenAccount};
    }
    
    
    [self.httpManager updatePersonalInfoParameterDict:parameterDict CompletionBlock:^(NSError *error)
     {
         [hud hideAnimated:YES];
         
         if (error)
         {
             [ErrorTipView errorTip:error.domain SuperView:self.view];
         }
         else
         {
             
             //微信是1qq是2*
             if ([type isEqualToString:@"1"])
             {
                 account.weChatUid = uid;
             }
             else if ([type isEqualToString:@"2"])
             {
                 account.qqUid = uid;
             }

             [account storeAccountInfo];
             [_tableview reloadData];
         }
         
     }];

}

@end
